import * as echarts from 'echarts';
import "echarts-gl";
import axios from "axios";

export async function ldbdxfqsFn() {
  const res = await axios.post("http://localhost:8080/x5");
  const data = res.data.data
  const chart = echarts.init(document.querySelector('.chart-ldbdxfqs') as HTMLElement);
  chart.setOption(
      {
        legend: {
          show: true,
          x: "center",
          y: 0,
          icon: "stack",
          itemWidth: 12,
          itemHeight: 6,
          itemGap: 50,
          textStyle: {
            color: "#D8DBDF",
            fontSize: 22
          },
          data: [
            "流量异常数",
            // "补丁安装数",
          ],
        },
        tooltip: {
          show: true,
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
          textStyle: {
            fontSize: 24,
          }
        },
        grid: {
          left: 0,
          right: 0,
          bottom: '3%',
          containLabel: true
        },
        // 设置颜色
        color: ['#3CFFDF', new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
          offset: 0,
          color: '#355FF2' // 渐变起始颜色
        },
          {
            offset: 1,
            color: '#43C8F6' // 渐变结束颜色
          }
        ])],
        // x轴数据
        xAxis: {
          type: 'category',
          data: [
            "2023-12",
            "2024-01",
            "2024-02",
            "2024-03",
            "2024-04"
          ],
          axisLine: {
            show: true,
            lineStyle: {
              color: "#384267",
            },
          },
          axisLabel: {
            fontSize: 18,
            color: '#D8DBDF'
          }
        },
        // y轴数据
        yAxis: {
          type: 'value',
          splitLine: {
            lineStyle: {
              color: '#384267',
              width: 1,
              type: 'dashed',
            },
            show: true,
          },
          axisLabel: {
            fontSize: 18,
            color: '#D8DBDF'
          }
        },
        // 折线图
        series: [
          //   {
          // name: '发现补丁',
          // type: 'line',
          // smooth: true, // 平滑曲线
          // data: [120, 155, 101, 134, 90, 230, 210],
          // lineStyle: {
          //   width: 4
          // }
        // },
          {
            name: '流量异常数',
            type: 'bar',
            barWidth: '16', // 柱子宽度
            data: data.values
          }
        ]
      },true
  );
};

export async function dhjkgjFn(val:any) {
  let res;
  const alertList:any[] = [];
  let series:any = [];
  if(val){
    res = await axios.post("http://localhost:8080/x11");
  }else{
    res = await axios.post("http://localhost:8080/x8");
  }
  const data = res.data.data;
  const xAxisData = data.names;
  const seriesData = data.values;
  if(val){
    series = [
      {
        name: val ? '流量异常数' : '次数',
        type: 'line',
        data: seriesData,
        smooth: true,
        showSymbol: false,
        lineStyle: {
          width: 4
        }
      }
    ]
  }else{
    series = [
      {
        name: val ? '流量异常数' : '次数',
        type: 'line',
        data: seriesData,
        smooth: true,
        showSymbol: false,
        lineStyle: {
          width: 4
        }
      }
    ]
    seriesData.forEach(()=>{
      alertList.push(30);
    })
    series.push({
      name: '警戒值',
      type: 'line',
      data: alertList,
      smooth: true,
      showSymbol: false,
      lineStyle: {
        width: 4,
        type: 'dashed'
      }
    })
  }
  const chart = echarts.init(document.querySelector('.chart-dhjkgj') as HTMLElement);
  chart.setOption({
    legend: {
      show: false,
      x: "center",
      y: 0,
      icon: "stack",
      itemWidth: 12,
      itemHeight: 6,
      itemGap: 50,
      textStyle: {
        color: "#D8DBDF",
        fontSize: 22
      },
      data: [
        "一级告警",
        "二级告警",
        "三级告警",
        "四级告警"
      ],
    },
    tooltip: {
      show: true,
      trigger: "axis",
      textStyle: {
        fontSize: 24,
      }
    },
    grid: {
      left: 0,
      right: 0,
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: xAxisData,
      axisLine: {
        show: true,
        lineStyle: {
          color: "#384267",
        },
      },
      axisLabel: {
        fontSize: 18,
        color: '#D8DBDF'
      }
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          color: '#384267',
          width: 1,
          type: 'dashed',
        },
        show: true,
      },
      axisLabel: {
        fontSize: 18,
        color: '#D8DBDF'
      }
    },
    series: series
  },true);
};

export async function gaojingqushiFn() {
  const chart = echarts.init(document.querySelector('.chart-gaojingqushi') as HTMLElement);
  // let military_statistics_data1 = [220, 100, 250, 145, 240, 130, 325, 155, 340, 250, 345, 240,];
  // let military_statistics_data2 = [310, 325, 155, 340, 110, 345, 240, 135, 325, 155, 340, 250,];
  let military_statistics_data1: any[] = [];
  let military_statistics_data2: any[] = [];
  const res = await axios.post("http://localhost:8080/x4");

  military_statistics_data1 = res.data.data[0].values
  military_statistics_data2 = res.data.data[1].values

  // const military_statistics_data3 = [345, 240, 135, 325, 155, 340, 250, 345, 240, 135, 325, 155];
  var fontColor = "#D8DBDF";
  var option = {
    grid: {
      left: 0,
      right: 30,
      top: 40,
      bottom: "5%",
      containLabel: true,
    },
    tooltip: {
      show: true,
      trigger: "axis",
      textStyle: {
        fontSize: 24,
      }
    },
    legend: {
      show: true,
      icon: "stack",
      itemWidth: 10,
      itemHeight: 10,
      itemGap: 50,
      textStyle: {
        color: "#D8DBDF",
        fontSize: 22
      },
      orient: 'horizontal', // 水平排列
      top: 0,
    },
    xAxis: [
      {
        type: "category",
        boundaryGap: false,
        axisLabel: {
          color: fontColor,
          fontSize: 18
        },
        axisLine: {
          show: true,
          lineStyle: {
            color: "#397cbc",
          },
        },
        axisTick: {
          show: false,
        },
        splitLine: {
          show: false,
          lineStyle: {
            color: "red",
          },
        },
        data: [
          "2023-12",
          "2024-01",
          "2024-02",
          "2024-03",
          "2024-04"
        ],
      },
    ],
    yAxis: [
      {
        type: "value",
        min: 0,
        // max: 400,
        axisLabel: {
          formatter: "{value}",
          textStyle: {
            color: fontColor,
            fontSize: 18
          },
        },
        axisLine: {
          lineStyle: {
            color: "#27b4c2",
          },
        },
        axisTick: {
          show: false,
        },
        splitLine: {
          lineStyle: {
            color: '#384267',
            width: 1,
            type: 'dashed',
          },
          show: true,
        },
      },
    ],
    series: [
      {
        name: "源主机到目标主机",
        type: "line",
        symbol: "circle",
        symbolSize: 8,
        smooth: true,

        itemStyle: {
          normal: {
            color: "#0092f6",
            lineStyle: {
              color: "#0092f6",
              width: 4,
            },
            areaStyle: {
              //color: '#94C9EC'
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: "rgba(7,44,90,0.3)",
                },
                {
                  offset: 1,
                  color: "rgba(0,146,246,0.9)",
                },
              ]),
            },
          },
        },
        markPoint: {
          itemStyle: {
            normal: {
              color: "red",
            },
          },
        },
        data: military_statistics_data1,
      },
      {
        name: "目标主机到源主机",
        smooth: true,
        type: "line",

        symbol: "circle",
        symbolSize: 8,

        itemStyle: {
          normal: {
            color: "#00d4c7",
            lineStyle: {
              color: "#00d4c7",
              width: 4,
            },
            areaStyle: {
              //color: '#94C9EC'
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: "rgba(7,44,90,0.3)",
                },
                {
                  offset: 1,
                  color: "rgba(0,212,199,0.9)",
                },
              ]),
            },
          },
        },
        data: military_statistics_data2,
      }
    ],
  };

  chart.setOption(option,true);
}

export async function wlgjlxFn() {
  const res = await axios.post("http://localhost:8080/x2");
  const data = res.data.data
  const chart = echarts.init(document.querySelector('.chart-wlgjlx') as HTMLElement);
  const option = {
    grid: {
      left: '0',
      right: "0",
      top: "0",
      bottom: "0",
      containLabel: true,
    },
    tooltip: {
      show: true,
      trigger: 'item',
      textStyle: {
        fontSize: 24,
      }
    },
    legend: [
      { show: false },
      { show: false },
      {
        show: true,
        selectedMode: true,
        orient: 'vertical',
        itemWidth: 10,
        itemHeight: 10,
        itemGap: 40,
        type: 'plain',
        right: '0%',
        top: 'center',
        // padding: [10, 0],
        formatter: function (name: string) {
          var value = option.series[2].data.find((item: any) => item.name === name).value;
          var percent = ((value / 100) * 10).toFixed(2) + '%';
          return `{a|${name + '：'}}  {b|${value}}   {c|${percent}}`;
        },
        textStyle: {
          rich: {
            a: {
              color: '#fff',
              fontSize: 28
            },
            b: {
              color: '#e0e0e0',
              // color: function (params:any) {
              //   console.log(params,"params")
              //   return 'red';
              // },
              fontSize: 28,
            },
            c: {
              color: '#57ffb2',
              fontSize: 28,
            }
          }
        },
        data: data.names,
      }
    ],
    series: [
      {
        name: '',
        type: 'pie',
        hoverAnimation: false,
        legendHoverLink: false,
        cursor: 'default',
        radius: ['52%', '70%'],
        center: ['30%', '50%'],
        color: [
          'rgba(255, 231, 119, 0.2)',
          'rgba(90, 175, 255, 0.2)',
          'rgba(55, 255, 201, 0.2)',
          'rgba(0, 132, 255, 0.2)',
          'rgba(25, 214, 255, 0.2)',
        ],
        label: {
          show: false,
        },
        labelLine: {
          show: false,
        },

        tooltip: {
          show: false,
        },

        zlevel: 1,
        itemStyle: {
          normal: {
            show: false,
          },
          ellipsis: {
            show: false,
          },
        },

        data: [
          {
            value: 20,
            name: '',
          },
          {
            value: 20,
            name: '',
          },
          {
            value: 20,
            name: '',
          },
          {
            value: 20,
            name: '',
          },
          {
            value: 20,
            name: '',
          },
        ],
      },
      {
        name: '',
        type: 'pie',
        zlevel: 2,
        cursor: 'default',
        hoverAnimation: false,
        legendHoverLink: false,
        radius: ['57%', '70%'],
        center: ['30%', '50%'],
        color: [
          'rgba(255, 231, 119, 0.5)',
          'rgba(90, 175, 255, 0.5)',
          'rgba(55, 255, 201, 0.5)',
          'rgba(0, 132, 255, 0.5)',
          'rgba(25, 214, 255, 0.5)',
        ],
        labelLine: {
          show: false,
        },
        itemStyle: {
          normal: {
            borderColor: '#0a1a2a',
          },
          ellipsis: {
            borderColor: '#0a1a2a',
          },
        },
        tooltip: {
          show: false,
        },
        data: [
          {
            value: 20,
            name: '',
          },
          {
            value: 20,
            name: '',
          },
          {
            value: 20,
            name: '',
          },
          {
            value: 20,
            name: '',
          }
        ],
      },
      {
        name: '攻击次数',
        type: 'pie',
        zlevel: 3,
        radius: ['70%', '80%'],
        center: ['30%', '50%'],
        color: ['rgba(255, 231, 119, 1)',
          'rgba(90, 175, 255, 1)',
          'rgba(55, 255, 201, 1)',
          'rgba(0, 132, 255, 1)',
          'rgba(25, 214, 255, 1)'],
        label: {
          show: true,
        },
        labelLine: {
          show: true,
        },
        itemStyle: {
          shadowBlur: 17,
          shadowColor: 'rgba(0, 0, 0, 0.3)',
          borderColor: '#0a1a2a',
        },
        data: [
          {
            value: data.dtos[0].value,
            name: data.dtos[0].name,
            color: 'rgba(255, 231, 119, 1)'
          },
          {
            value: data.dtos[1].value,
            name: data.dtos[1].name,
            color: 'rgba(90, 175, 255, 1)'
          },
          {
            value: data.dtos[2].value,
            name: data.dtos[2].name,
            color: 'rgba(55, 255, 201, 1)'
          },
          {
            value: data.dtos[3].value,
            name: data.dtos[3].name,
            color: 'rgba(0, 132, 255, 1)'
          },
          {
            value: data.dtos[4].value,
            name: data.dtos[4].name,
            color: 'rgba(25, 214, 255, 1)'
          }
        ],
      },
    ],
  };
  chart.setOption(option,true);
  return option
}

export function energyProportion(selector: string, value: number, barColor: string) {
  const chart = echarts.init(document.querySelector(selector) as HTMLElement);
  const option = {
    // title: [{
    //     text: '118',
    //     // x: '50%',
    //     // y: '46%',
    //     textAlign: 'center',
    //     textStyle: {
    //         fontSize: '40',
    //         fontWeight: '100',
    //         color: '#D7E9F9',
    //         textAlign: 'center',
    //     },
    // }],
    polar: {
      radius: ['85%', '73%'],
      center: ['50%', '50%'],
    },
    angleAxis: {
      max: 100,
      show: false,
    },
    radiusAxis: {
      type: 'category',
      show: true,
      axisLabel: {
        show: false,
      },
      axisLine: {
        show: false,

      },
      axisTick: {
        show: false
      },
    },
    series: [{
      name: '',
      type: 'bar',
      startAngle: 90,
      roundCap: false,
      barWidth: 60,
      showBackground: true,
      backgroundStyle: {
        color: '#43607a75',
      },
      data: [value],
      coordinateSystem: 'polar',
      itemStyle: {
        normal: {
          color: barColor
        }
      }
    }

    ]
  };

  chart.setOption(option);
}
let wlaqzels_data = [
  { value: 20, valueText: 1, name: '公司网络安全责任书', itemStyle: { color: '#FFCA7A' } },
  { value: 40, valueText: 27, name: '部门网络安全责任书', itemStyle: { color: '#36DC88' } },
  { value: 60, valueText: 976, name: '保密安全承诺书', itemStyle: { color: '#4C9DFF' } },
  // { value: 80, valueText: 31008, name: '网络安全承诺书', itemStyle: { color: '#346FF5' } }
]
export async function wlaqzelsFn() {
  const res = await axios.post("http://localhost:8080/x1");
  const data = res.data.data
  // 计算总攻击次数
  const totalAttacks = data.values.reduce((acc:any, val:any) => acc + val, 0);
  // 计算每个协议类型的比例
  const proportionData = data.dtos.map((item:any) => {
    return {
      ...item,
      proportion: (item.value / totalAttacks) * 100 // 计算比例并转换为百分比
    };
  });
  // 根据比例进行排序
  proportionData.sort((a:any, b:any) => a.value - b.value);
  // 更新数据结构
  data.dtos = proportionData;
  data.dtos.forEach((item:any,index:number)=>{
    wlaqzels_data[index].name = item.name
    wlaqzels_data[index].valueText = item.value
  })
  const chart = echarts.init(document.querySelector('.chart-wlaqzels') as HTMLElement);
  const option = {
    legend: {
      show: false,
      data: data.names,
      textStyle: {
        color: "#D8DBDF",
        fontSize: 22
      },
    },
    series: [
      {
        name: 'Funnel',
        type: 'funnel',
        left: '0',
        top: 10, // 调整 top 属性的值
        bottom: 60, // 调整 bottom 属性的值
        width: '55%',
        min: 0,
        max: 100,
        minSize: '0%',
        maxSize: '100%',
        sort: 'ascending', // 调整 sort 属性为 'ascending'
        gap: 0,
        itemStyle: {
          borderColor: '#ffffff00',
          borderWidth: 1
        },
        label: {
          show: false
        },
        data: wlaqzels_data
      }
    ]
  };
  chart.setOption(option);
  return wlaqzels_data;
}


export async function initAbnormalEventLocationRelationshipData() {
  const chart = echarts.init(document.querySelector('.chart-abnormal-event-location-relationship') as HTMLElement);
  // let military_statistics_data1 = [220, 100, 250, 145, 240, 130, 325, 155, 340, 250, 345, 240,];
  // let military_statistics_data2 = [310, 325, 155, 340, 110, 345, 240, 135, 325, 155, 340, 250,];
  let military_statistics_data: any[] = [];
  let military_statistics_names: any[] = [];
  const res = await axios.post("http://localhost:8080/x10");

  military_statistics_data = res.data.data.values
  military_statistics_names = res.data.data.names

  let data = [
    { value: 62, name: "视频广告" },
    { value: 243, name: "搜索引擎" }
  ];

  // const military_statistics_data3 = [345, 240, 135, 325, 155, 340, 250, 345, 240, 135, 325, 155];
  var fontColor = "#D8DBDF";
  var option = {
    grid: {
      left: 0,
      right: 50,
      top: 60,
      bottom: "5%",
      containLabel: true,
    },
    tooltip: {
      show: true,
      trigger: "axis",
      textStyle: {
        fontSize: 24,
      }
    },
    legend: {
      show: false,
      icon: "stack",
      itemWidth: 10,
      itemHeight: 10,
      itemGap: 50,
      textStyle: {
        color: "#D8DBDF",
        fontSize: 22
      },
      orient: 'horizontal', // 水平排列
      top: 0,
    },
    xAxis: [
      {
        type: "category",
        boundaryGap: false,
        axisLabel: {
          color: fontColor,
          fontSize: 18
        },
        axisLine: {
          show: true,
          lineStyle: {
            color: "#397cbc",
          },
        },
        axisTick: {
          show: false,
        },
        splitLine: {
          show: false,
          lineStyle: {
            color: "red",
          },
        },
        data: military_statistics_names
      },
    ],
    yAxis: [
      {
        type: "value",
        min: 0,
        // max: 400,
        axisLabel: {
          formatter: "{value}",
          textStyle: {
            color: fontColor,
            fontSize: 18
          },
        },
        axisLine: {
          lineStyle: {
            color: "#27b4c2",
          },
        },
        axisTick: {
          show: false,
        },
        splitLine: {
          lineStyle: {
            color: '#384267',
            width: 1,
            type: 'dashed',
          },
          show: true,
        },
      },
    ],
    series: [
      {
        name: "位置",
        type: "line",
        symbol: "circle",
        symbolSize: 8,
        smooth: true,

        itemStyle: {
          normal: {
            color: "#0092f6",
            lineStyle: {
              color: "#0092f6",
              width: 4,
            },
            areaStyle: {
              //color: '#94C9EC'
              color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                {
                  offset: 0,
                  color: "rgba(7,44,90,0.3)",
                },
                {
                  offset: 1,
                  color: "rgba(0,146,246,0.9)",
                },
              ]),
            },
          },
        },
        markPoint: {
          itemStyle: {
            normal: {
              color: "red",
            },
          },
        },
        data: military_statistics_data,
      },
    ],
  };

  chart.setOption(option);
}

export async function sjlxblfxFn() {
  const res = await axios.post("http://localhost:8080/x12");
  const data = res.data.data;
  const chart = echarts.init(document.querySelector('.chart-sjlxblfx') as HTMLElement);
  // 饼图数据
  let optionData:any[] = []
  data.dtos.forEach((item:any)=>{
    optionData.push({
      name: item.name,
      value: item.value
    })
  })
  const option = getPie3D(optionData, 0);
  chart.setOption(option,true);
}

export async function gjlxfbbFn(val:boolean) {
  let num = 0;
  let data:any[] = [];
  let currentIndex = 0;
  let oldIndex;
  let barChartList:any[] = []
  let res

  if(val){
    res = await axios.post("http://localhost:8080/x3");
  }else{
    res = await axios.post("http://localhost:8080/x9");
  }

  const result = res.data.data;
  result.dtos.forEach((item:any)=>{
    barChartList.push({
      name: item.name,
      value: item.value
    })
    num += item.value;
  })
  const chart = echarts.init(document.querySelector('.chart-gjlxfbb') as HTMLElement);
  var color = ['#006EDF', '#00FF00', '#FFC30D', '#FF8400'];
  for (var i = 0; i < barChartList.length; i++) {
    data.push(
        {
          value: barChartList[i].value,
          name: barChartList[i].name,
          itemStyle: {
            normal: {
              borderWidth: 6,
              shadowBlur: 10,
              borderRadius: 10, // 圆角
              borderColor: color[i],
              shadowColor: color[i],
              color: color[i],
            }
          }
        },
        {
          value: 4,
          name: '',
          itemStyle: {
            normal: {
              label: {
                show: false
              },
              labelLine: {
                show: false
              },
              color: 'rgba(0, 0, 0, 0)',
              borderColor: 'rgba(0, 0, 0, 0)',
              borderWidth: 0
            }
          }
        }
    );
  }
  var seriesOption = [
    {
      name: 'pie2',
      type: 'pie',
      radius: [140, 150],
      center: ['50%', '42%'],
      color: '#068898',
      label: {
        show: false
      },
      labelLine: {
        show: false
      },
      data: [1],
      silent: true
    },
    {
      name: val ? '攻击类型分布比' : '异常日志事件比',
      center: ['50%', '42%'],
      type: 'pie',
      radius: [160, 170],
      label: {
        show: false,
        position: 'center',
        // formatter: `{c_style|${num}%}\n{b_style|{b}}`,
        formatter: function (param: any) {
          var value = data.find((item: any) => item.name === param.name);
          var percent = ((value.value / num) * 100).toFixed(2) + '%';
          return `{a|${param.name}} \n  {b|${percent}}`;
        },
        rich: {
          a: {
            fontSize: 32,
            fontWeight: 400,
            color: '#fff'
          },
          b: {
            padding: [20, 0, 0, 0],
            fontSize: 32,
            fontWeight: 'bold',
            color: '#fff'
          }
        }
      },
      emphasis: {
        label: {
          show: true,
          fontSize: '14',
          fontWeight: 'normal'
        }
      },
      labelLine: {
        show: false
      },
      data: data
    }
  ];
  let option = {
    color: color,
    tooltip: {
      show: true,
      trigger: "item",
      textStyle: {
        fontSize: 24,
      }
    },
    legend: {
      show: true,
      icon: "stack",
      hoverAnimation: false,
      legendHoverLink: false,
      selectedMode: false,
      textStyle: {
        color: "#D8DBDF",
        fontSize: 22
      },
      orient: 'vertical', // 水平排列
      bottom: 28,
      left: 'center'
    },
    series: seriesOption
  };

  chart.on('mouseover', (params) => {
    oldIndex = currentIndex;
    currentIndex = params.dataIndex;
    highlightPie(currentIndex,oldIndex);
  })

  function highlightPie(currentIndex:any,oldIndex:any) {
    chart.dispatchAction({
      type: 'downplay',
      seriesIndex: 1,
      dataIndex: oldIndex
    })
    chart.dispatchAction({
      type: 'highlight',
      seriesIndex: 1,
      dataIndex: currentIndex
    })
  }

  setTimeout(()=>{
    highlightPie(0,1);
  },50);

  chart.setOption(option,true);
}

/**
 * getParametricEquation 生成扇形的曲面参数方程 生成 3D 扇形环曲面
 * @param{} startRatio（浮点数）: 当前扇形起始比例，取值区间 [0, endRatio)
 * @param{} endRatio（浮点数）: 当前扇形结束比例，取值区间 (startRatio, 1]
 * @param{} isSelected（布尔值）:是否选中，效果参照二维饼图选中效果（单选）
 * @param{} isHovered（布尔值）: 是否放大，效果接近二维饼图高亮（放大）效果（未能实现阴影）
 * @param{} k（0~1之间的浮点数）：用于参数方程的一个参数，取值 0~1 之间，通过「内径/外径」的值换算而来。
 * @param{} h :饼图的初始高度
 */
function getParametricEquation(startRatio:any, endRatio:any, isSelected:any, isHovered:any, k:any, h:any) {
  const midRatio = (startRatio + endRatio) / 2;
  const startRadian = startRatio * Math.PI * 2;
  const endRadian = endRatio * Math.PI * 2;
  const midRadian = midRatio * Math.PI * 2;
  // 如果只有一个扇形，则不实现选中效果。
  if (startRatio === 0 && endRatio === 1) {
    isSelected = false;
  }
  k = typeof k !== "undefined" ? k : 1 / 3;
  const offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0;
  const offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;
  // 鼠标滑过时外环放大大小
  const hoverRate = isHovered ? 1.05 : 1;
  // 返回曲面参数方程
  return {
    u: { min: -Math.PI, max: Math.PI * 3, step: Math.PI / 32 },
    v: { min: 0, max: Math.PI * 2, step: Math.PI / 20 },

    x(u:any, v:any) {
      if (u < startRadian) { return offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate; }
      if (u > endRadian) { return offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate; }
      return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
    },

    y(u:any, v:any) {
      if (u < startRadian) { return offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate; }
      if (u > endRadian) { return offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate; }
      return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
    },

    z(u:any, v:any) {
      if (u < -Math.PI * 0.5) { return Math.sin(u); }
      if (u > Math.PI * 2.5) { return Math.sin(u) * h * 0.1; }
      // 当前图形的高度是Z根据h（每个value的值决定的）
      return Math.sin(v) > 0 ? 1 * h * 0.1 : -1;
    }
  };
}

// 可做为调整内环大小 0为实心圆饼图，大于0 小于1 为圆环
function getPie3D(pieData:any, internalDiameterRatio:any) {
  const series:any = [];
  let sumValue = 0;
  let startValue = 0;
  let endValue = 0;
  const legendData = [];
  const k = typeof internalDiameterRatio !== "undefined" ? (1 - internalDiameterRatio) / (1 + internalDiameterRatio) : 1 / 3;
  for (let i = 0; i < pieData.length; i += 1) {
    sumValue += pieData[i].value;
    const seriesItem:any = {
      name: typeof pieData[i].name === "undefined" ? `series${i}` : pieData[i].name,
      type: "surface",
      parametric: true,
      wireframe: { show: false },
      pieData: pieData[i],
      pieStatus: { selected: false, hovered: false, k }
    };
    if (typeof pieData[i].itemStyle !== "undefined") {
      const { itemStyle } = pieData[i];
      // eslint-disable-next-line no-unused-expressions
      typeof pieData[i].itemStyle.color !== "undefined" ? (itemStyle.color = pieData[i].itemStyle.color) : null;
      // eslint-disable-next-line no-unused-expressions
      typeof pieData[i].itemStyle.opacity !== "undefined" ? (itemStyle.opacity = pieData[i].itemStyle.opacity) : null;
      seriesItem.itemStyle = itemStyle;
    }
    series.push(seriesItem);
  }
  for (let i = 0; i < series.length; i += 1) {
    endValue = startValue + series[i].pieData.value;
    series[i].pieData.startRatio = startValue / sumValue;
    series[i].pieData.endRatio = endValue / sumValue;
    series[i].parametricEquation = getParametricEquation(
        series[i].pieData.startRatio,
        series[i].pieData.endRatio,
        false,
        false,
        k,
        10 // 在此处传入饼图初始高度h
    );
    startValue = endValue;
    legendData.push(series[i].name);
  }
  // 准备待返回的配置项，把准备好的series 传入。
  const option = {
    legend: {
      show: true,
      type: "scroll",
      right: 3,
      top: 'center',
      orient: "vertical", // 纵向
      icon: "circle", // icon 类型
      itemHeight: 12, // icon高度
      itemWidth: 12, // icon 宽度
      itemGap: 5, // 图例间隔
      textStyle: {
        color: "#709DD9",
        fontSize: 28,
        fontWeight: "400"
      },
      formatter: (name:any) => {
        if (pieData.length) {
          const item = pieData.filter((item:any) => item.name === name)[0];
          return `  ${name}：${item.value}`;
        }
      }
    },
    color: ["#159AFF", "#66E1DF", "#66E193", "#E3F170", "#FFAD6A", "#ffe0ab"],
    tooltip: {
      textStyle: {
        fontSize: 24,
      },
      formatter: (params:any) => {
        if (params.seriesName !== "mouseoutSeries") {
          return `${params.marker}${params.seriesName}：${pieData[params.seriesIndex].value}`;
        }
        return "";
      }
    },
    xAxis3D: { min: -1, max: 1 },
    yAxis3D: { min: -1, max: 1 },
    zAxis3D: { min: -1, max: 1 },
    grid3D: {
      show: false,
      boxHeight: 15, // 修改立体饼图的高度
      top: "-10%",
      left: "-15%",
      viewControl: {
        // 3d效果可以放大、旋转等，
        alpha: 20, // 饼图翻转的程度
        beta: 30,
        rotateSensitivity: 1,
        zoomSensitivity: 0,
        panSensitivity: 0,
        autoRotate: true, // 是否自动旋转
        distance: 240 // 距离越小看到的饼图越大
      }
    },
    series
  };
  return option;
}
